package com.flexycore.iphone.controller;

import java.util.Vector;

import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.uikit.NSIndexPath;
import com.flexycore.iphone.uikit.UIFont;
import com.flexycore.iphone.uikit.UITableView;
import com.flexycore.iphone.uikit.UITableViewCell;
import com.flexycore.iphone.uikit.UITableViewCellEditingStyle;
import com.flexycore.iphone.uikit.UITableViewController;
import com.flexycore.iphone.uikit.UITableViewRowAnimation;
import com.flexycore.iphone.uikit.UITableViewStyle;

/**
 * 
 * @author matthieu nicolazo
 * 
 * Creation of fonts names list available in iPhone SDK.
 * This list can be edited by deleting rows.
 *
 */
public class DeleteCellsController  extends UITableViewController {
	/**
	 * String table containing all font families names.
	 */
	private Vector<String> fontNames;
	
	
	/**
	 * Constructor
	 */
	public DeleteCellsController(){
		super();
		initWithStyle(UITableViewStyle.UITableViewStylePlain);
		setTitle("Font families");

		// Initialize fontNames vector
		fontNames = UIFont.familyNames();
		
		// Put an "edit" button on the right side of navigation bar.
		this.navigationItem().setRightBarButtonItemAnimated(this.editButtonItem(), true);
	}
	
	/**
	 * @param	table		UITableView calling this method.
	 */
	@Override
	public int numberOfSectionsInTableView(UITableView table){
		return 1;
	}

	/**
	 * @param	table		UITableView calling this method.
	 * @param	section		Section in which rows will be displayed.
	 * @return				The number of rows in section.
	 */
	@Override
	public int tableViewNumberOfRowsInSection(UITableView table, int section){
		return fontNames.size();
	}
	
	/**
	 * @return UITableViewCell	cell displaying in index section and row.
	 */
	@Override
	public UITableViewCell tableViewCellForRowAtIndexPath(UITableView t, NSIndexPath indexPath){
		UITableViewCell cell = new UITableViewCell();
		cell.initWithFrame(CGRect.CGRectZero());
		String txt = fontNames.get(indexPath.row());
		cell.setText(txt);
		cell.setShowsReorderControl(true);
		return cell;
	}
	
	/**
	 * @param	tableView		UITableView calling this method.
	 * @param	indexPath		Section and row of cell.
	 * @return					UITableViewCellEditingStyle for this cell.
	 */
	@Override
	public int tableViewEditingStyleForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		return UITableViewCellEditingStyle.UITableViewCellEditingStyleDelete;
	}
	
	/**
	 * 
	 */
	@Override
	public void tableViewCommitEditingStyleForRowAtIndexPath( UITableView tableView, int editingStyle, 
			NSIndexPath indexPath) {
		// In order to edit tableView, all modifications have to be contained between "beginUpdates()"
		// and "endUpdates()".
		this.tableView().beginUpdates();
		
		// Delete cell at indexPath
		if(editingStyle == UITableViewCellEditingStyle.UITableViewCellEditingStyleDelete){
			Vector<NSIndexPath> v = new Vector();
			v.add(indexPath);
			tableView.deleteRowsAtIndexPathsWithRowAnimation(v, UITableViewRowAnimation.UITableViewRowAnimationRight);
			fontNames.removeElementAt(indexPath.row());
		}
		
		this.tableView().endUpdates();
	}
	
}
